package com.living.player.ui.recently.audio;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.Constant;
import com.living.R;
import com.living.filedownloader.DownLoadManger;
import com.living.filedownloader.bean.TasksManagerModel;
import com.living.player.PlayUtils;
import com.living.player.db.AudioPlayListManger;
import com.living.player.db.RecentlyManger;
import com.living.player.ui.recently.adapter.RecentlyAudioAdapter;
import com.living.widget.MorePopupWindow;
import com.md.basedpc.base.BaseFragment;
import com.md.basedpc.persistence.PHSPUtil;
import com.md.livingwidget.emptyview.PHEmptyViewUI;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecentlyAudioFragment extends BaseFragment implements OnItemClickListener, OnItemChildClickListener, View.OnClickListener {

    View layout_recently_audio_edite;
    TextView tv_recnetly_play;
    private RecentlyAudioAdapter taskItemAdapter;
    private ArrayList<Audio> downloadedList = new ArrayList<>();
    MorePopupWindow morePopupWindow;

    @Override
    protected int getLayout() {
        return R.layout.fragment_player_recently_audio;
    }

    /**
     * 移除操作
     *
     * @param t
     */
    public void remove(Audio t) {
        downloadedList.remove(t);
        initData();
        if (taskItemAdapter.getData().size() == 0) {
            getActivity().finish();
        }
    }

    public void setDownloadedList(ArrayList<Audio> downloadedList) {
        this.downloadedList = downloadedList;
        taskItemAdapter.setNewInstance(downloadedList);
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        layout_recently_audio_edite = view.findViewById(R.id.layout_recently_audio_edite);
        tv_recnetly_play = view.findViewById(R.id.tv_recnetly_play);
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        taskItemAdapter = new RecentlyAudioAdapter();
        recyclerView.setAdapter(taskItemAdapter);

        view.findViewById(R.id.iv_recently_audio_pause).setOnClickListener(this);
        view.findViewById(R.id.tv_recnetly_play).setOnClickListener(this);
        view.findViewById(R.id.iv_recnetly_audio_edite).setOnClickListener(this);
        taskItemAdapter.setOnItemClickListener(this);
        taskItemAdapter.setOnItemChildClickListener(this);

        PHEmptyViewUI phEmptyViewUI = new PHEmptyViewUI(getActivity());
        taskItemAdapter.setEmptyView(phEmptyViewUI);

        taskItemAdapter.setNewInstance(downloadedList);

        morePopupWindow = new MorePopupWindow(getActivity(), new MorePopupWindow.OnMoreItemClick() {
            @Override
            public void onClickAdd(Object object) {
                Audio audio = (Audio) object;

            }

            @Override
            public void onClickDownload(Object object) {
                Audio audio = (Audio) object;
//                DownLoadManger.getInstance().downloadAudioList();
            }

            @Override
            public void onClickShare(Object object) {
                Audio audio = (Audio) object;
            }

            @Override
            public void onClickDelete(Object object) {
                Audio audio = (Audio) object;
                taskItemAdapter.remove(audio);
                DownLoadManger.getInstance().deleteAudio(audio.getAlbumId(), audio.getId(), audio.getAudioUrl());
            }
        });
        morePopupWindow.setMenuShows(true, true, true, true);

        initData();

        return view;
    }

    private void initData() {
        List<Audio> audioList = RecentlyManger.getInstance().getAllRecentAudios();
        taskItemAdapter.setNewInstance(audioList);
        tv_recnetly_play.setText("播放全部(" + audioList.size() + ")");
        layout_recently_audio_edite.setVisibility(audioList.size() == 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.iv_recently_audio_pause || _id == R.id.tv_recnetly_play) {

        } else if (_id == R.id.iv_recnetly_audio_edite) {
            getActivity().startActivityForResult(new Intent(getActivity(), RecentlyAudioBatchActivity.class), 123);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Audio audio = taskItemAdapter.getItem(position);
        PlayUtils.addAudioListPlay(getActivity(), audio, taskItemAdapter.getData());
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        morePopupWindow.showDialog(taskItemAdapter.getItem(position));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            initData();
        }
    }


}
